//
//  AudioTypeViewController.m
//  AudioBook_A
//
//  Created by Sansan on 15/4/16.
//  Copyright (c) 2015年 Sansan. All rights reserved.
//

#import "AudioTypeViewController.h"

@interface AudioTypeViewController ()

@end

@implementation AudioTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.title;
    
    UIImage *image = [UIImage imageNamed:@"back.png"];
    // 取消渲染
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    
    UIImage *findImage = [UIImage imageNamed:@"find.png"];
    findImage = [findImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:findImage style:UIBarButtonItemStylePlain target:self action:@selector(findAction)];

    self.view.backgroundColor = [UIColor colorWithRed:152/255.0 green:185/255.0 blue:200/255.0 alpha:0.8];
    
    // 苹果系统自带一个标准瀑布流
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置item大小
    flowlayout.itemSize = CGSizeMake(110, 110);
    // 设置活动方向(上下滑动)
    flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 单元格间距
    // 列
    flowlayout.minimumInteritemSpacing = 5;
    // 行
    flowlayout.minimumLineSpacing = 10;
    
    
    
    
    // 集合视图
    self.audioTypeCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 10, ScreenFram.width - 20, ScreenFram.height - 64) collectionViewLayout:flowlayout];
    self.audioTypeCollectionView.backgroundColor = [UIColor colorWithRed:152/255.0 green:185/255.0 blue:200/255.0 alpha:0.8];
    [self.view addSubview:self.audioTypeCollectionView];
    [self.audioTypeCollectionView release];
    
    
    self.audioTypeCollectionView.delegate = self;
    self.audioTypeCollectionView.dataSource = self;
    
    
    // 注册cell
    [self.audioTypeCollectionView registerClass:[AudioTypeCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.audioTypeCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    [self.audioTypeCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    
    
    self.allModelArray = [NSMutableArray array];
    
    NSString *url = [NSString stringWithFormat:URLType, self.urlKey];
    
    [MyAFNData getNetData:url responseGETData:^(id data) {
        
        NSMutableArray *arr = [data objectForKey:@"list"];
        for (NSMutableDictionary *dic in arr) {
            
            AudioTypeModel *model = [[AudioTypeModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.allModelArray addObject:model];
            
        }
        // 刷新
        [self.audioTypeCollectionView reloadData];
        
    }];
    
    
    
    // Do any additional setup after loading the view.
}

- (void)findAction
{
    SearchBarViewController *searchVC = [[SearchBarViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
    [searchVC release];
}

- (void)backAction
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

// 两个必须实现的方法之一
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.allModelArray.count;
}

// 两个必须实现的方法之一
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 不用重用池,自带
    // 创建UICollectionViewCell显示内容
    AudioTypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0f];
    
    AudioTypeModel *model = [self.allModelArray objectAtIndex:indexPath.row];
    
    cell.model = model;
    
    // 添加轻拍手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    cell.contentView.tag = 1000 + indexPath.row;
    [cell.contentView addGestureRecognizer:tap];
    
    return cell;
}


- (void)tapAction:(UITapGestureRecognizer *)tap
{
    NSInteger key = tap.view.tag % 1000;
    
    NSString *tname = ((AudioTypeModel *)[self.allModelArray objectAtIndex:key]).tname;
    AudioListViewController *typeVC = [[AudioListViewController alloc] init];
    typeVC.navigationItem.title = tname;
    // 属性传值 把tname传过来
    typeVC.tname = tname;
    typeVC.urlKey = self.urlKey;
    // push 进入NovelList页面
    [self.navigationController pushViewController:typeVC animated:YES];
    [typeVC release];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
